package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public Result getList(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(required = false) String name,
                          @RequestParam(required = false) Integer gender,
                          @RequestParam(required = false) LocalDate begin,
                          @RequestParam(required = false) LocalDate end
    ){

        PageBean empList = empService.getList(page, pageSize,name,gender,begin,end);
        return Result.success(empList);
    }

    @GetMapping("/list")
    public Result getEmpList(){
        List<Emp> list = empService.getTeacherList();
        return Result.success(list);
    }

    /**
     * 添加部门
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("请求参数emp: {}", emp);
        empService.save(emp);
        return Result.success();
    }

    /**
     * 根据ID删除部门
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("批量删除员工信息：{}",ids);
        empService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据ID查询
     * @param id
     * @return Emp
     */
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id){
        log.info("根据id查询：{}",id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    /**
     * 修改部门
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }
}
